package uno.linze.wallpaper.domain.identity.controller;

import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import uno.linze.wallpaper.common.exception.BusinessException;
import uno.linze.wallpaper.domain.identity.dto.UserRegister;
import uno.linze.wallpaper.domain.identity.service.UsersService;
import uno.linze.wallpaper.domain.identity.service.ext.AuthService;
import uno.linze.wallpaper.domain.identity.service.ext.VerifyCodeService;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    VerifyCodeService verifyCodeService;

    @Autowired
    UsersService usersServiceImpl;

    @Autowired
    AuthService authService;

    @GetMapping("/hello")
    public ResponseEntity<String> hello() {
        UserRegister userRegister = new UserRegister("linze", "12312312", "password123","1234");
        //打印日志
        return ResponseEntity.ok(userRegister.toString());
    }
    @PostMapping("/registervarifycode")
    public ResponseEntity<String> registerVarifyCode(
            @RequestBody Map<String, Object> message
            ) {

        String email = (String) message.get("email");
        if (StrUtil.isBlank(email) || !email.contains("@")) {
            return ResponseEntity.status(500).body("邮箱格式错误");
        }
        try {
            verifyCodeService.sendAndSaveCode(email, 5);
            return ResponseEntity.ok("验证码已发送");
        } catch (Exception e) {
            return ResponseEntity.status(500).body("发送失败：" + e.getMessage());
        }
    }
    @PostMapping("/register")
    public ResponseEntity<String> register(
            @RequestBody UserRegister userRegister
    ) {
        try {
            usersServiceImpl.register(userRegister);
            return ResponseEntity.ok("注册成功");
        } catch (BusinessException e) {
            return ResponseEntity.status(500).body("注册失败：" + e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.status(500).body("注册失败：" + e.getMessage());
        }
    }

    @PostMapping("/login")
    public ResponseEntity<String> login(
            @RequestBody Map<String, Object> message
    ) {
        String username = (String) message.get("username");
        String password = (String) message.get("password");
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            return ResponseEntity.status(500).body("用户名或密码不能为空");
        }
        try {
            String token = authService.loginAndGenerateToken(username, password);
            return ResponseEntity.ok(token);
        } catch (BusinessException e) {
            return ResponseEntity.status(500).body("登录失败：" + e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.status(500).body("登录失败：" + e.getMessage());
        }
    }


}
